﻿using System;

namespace Library.OrmEnterpriseEntity.Factor {
	/// <summary>
	/// Sinleton context
	/// </summary>
	public class DbContext {
		static DbContext() {
			Instance = new DbContext();
		}
		private DbContext () {
		}
		/// <summary>
		/// Sinleton instance
		/// </summary>
		public static DbContext Instance { get; private set; }
		/// <summary>
		/// Init context for application.  Call this method when application start and before any access database action
		/// </summary>
		/// <param name="dbContext"></param>
		public static void ApplicationInit(IDbContext dbContext) {
			Instance._context = dbContext;
		}
		
		private IDbContext _context;
		/// <summary>
		/// Get current context for this application
		/// </summary>
		public IDbContext Context {
			get {
				
				if (_context == null) 
					throw new Exception("Call DbContext.ApplicationInit method first");
				return _context;
			}
		}
	}
}
